import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/common/theme/app_colors.dart';
import 'package:flutter_taotaoshipin/util/device_utils.dart';
import 'package:flutter_taotaoshipin/widget/gaps.dart';
import 'package:flutter_taotaoshipin/widget/my_button.dart';
import 'package:get/get.dart';

/// 自定义dialog的模板
class BaseDialog extends StatelessWidget {
  const BaseDialog(
      {super.key,
      this.title,
      required this.child,
      this.onPressed,
      this.confirmBtnTitle});

  final String? title;
  final Widget child;
  final String? confirmBtnTitle;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    /// Android 11添加了键盘弹出动画，这与我添加的过渡动画冲突（原先iOS、Android 没有相关过渡动画，相关问题跟踪：https://github.com/flutter/flutter/issues/19279）。
    /// 因为在Android 11上，viewInsets的值在键盘弹出过程中是变化的（以前只有开始结束的值）。
    /// 所以解决方法就是在Android 11及以上系统中使用Padding代替AnimatedPadding。
    if (Device.getAndroidSdkInt() >= 30) {
      return Padding(
        padding: context.mediaQueryViewInsets,
        child: _buildContent(context),
      );
    } else {
      return AnimatedPadding(
        padding: context.mediaQueryViewInsets,
        duration: const Duration(milliseconds: 120),
        curve: Curves.easeInCubic,
        child: _buildContent(context),
      );
    }
  }

  _buildContent(BuildContext context) {
    return MediaQuery.removeViewInsets(
      context: context,
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      child: Center(
        child: SizedBox(
          width: 270,
          child: _buildBody(context),
        ),
      ),
    );
  }

  _buildBody(BuildContext context) {
    return Material(
      borderRadius: BorderRadius.circular(10),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Visibility(
            visible: title != null,
            child: Padding(
              padding: const EdgeInsets.only(top: 17),
              child: Text(
                title ?? '',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          child,
          // 底部按钮
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              GestureDetector(
                onTap: () => Get.back(),
                child: Container(
                  width: 115,
                  height: 39,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color(0xFFECEDF1),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    '取消',
                    style: TextStyle(
                      color: Color(0xFFAEBCDB),
                      fontSize: 16,
                    ),
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Get.back();
                  onPressed?.call();
                },
                child: Container(
                  width: 115,
                  height: 39,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: AppColors.app_main,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    confirmBtnTitle ?? '确定',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 13),
        ],
      ),
    );
  }
}
